Skip to content

fix(dracut-install): bounds-check RUNPATH walk and ELF string derefs - #2622

Draft
prabhakarpujeri wants to merge 4 commits into
dracut-ng:mainfrom
prabhakarpujeri:fix/elf-runpath-bounds
Draft

prabhakarpujeri wants to merge 4 commits into
dracut-ng:mainfrom
prabhakarpujeri:fix/elf-runpath-bounds

Conversation

@prabhakarpujeri

Copy link
Copy Markdown

Stacked on #2615 (depends on its PARSE_ELF_START extent checks and elf_sect_name helper). Will be rebased after #2615 merges.

Problem

FIND_LIBRARY_RUNPATH_FOR_BITS (in src/install/dracut-install.c) walks the ELF .dynamic section's Elf_Dyn array and pulls out the DT_RUNPATH / DT_RPATH strings to search for libraries. Unlike the sibling macros RESOLVE_DEPS_NEEDED_FOR_BITS and RESOLVE_DEPS_DLOPEN_FOR_BITS, this walk validates only e_shoff/e_shstrndx — the per-iteration pointers into the map are never bounds-checked. Additionally, in the interpreter/DT_SONAME/DT_NEEDED code paths the strings extracted with (char *)map + shdr[i].sh_offset + d->d_un.d_val are handed to strlen/strdup/hashmap_get/expand_runpath/regexec without verifying they are NUL-terminated within the mapped file (a malformed ELF reading past the end of the map can hang on a never-terminated strcmp/strdup).

Also found one off-by-boundary check in the interpreter path: interpreter > (char *)map + src_len accepts the pointer at exactly one-past-end (should be >=).

Fix

  • New shared helper elf_map_string(map, src_len, offset): returns NULL if the offset is outside the map or the string there isn't NUL-terminated within the map. Used for the runpath string, interpreter string, DT_SONAME and DT_NEEDED strings.
  • The RUNPATH walk now mirrors its siblings' per-iteration bounds checks on dyn and d.

Verification

Crafted 3 malformed ELF64 inputs exercising each new check; ran the real dracut-install -l binary built before/after, and valgrind on the difference-maker:

input before after
H7A2: DT_RPATH whose strtab pointer lands 4 bytes before map end, runpath not NUL-terminated valgrind Invalid read of size 1 → SIGSEGV clean exit 0 (section rejected)
H7B2: PT_INTERP at exactly src_len one-past-end no crash (never dereferenced) clean exit 0 (no change needed⁠—it's never actually reached the new helper with that offset as a valid string; check now correctly excludes)
H7C2: PT_INTERP in-map but non-terminated clean clean exit 0

(The last two are safe in both versions only because no code path dereferences them — they're defensive/paranoia checks. The RUNPATH one is the reachable crash.)

Regression control: -l dependency resolution of /bin/bash, /bin/ls, and /usr/bin/ssh — byte-identical 33-entry file list before vs after. H6 test set re-verified — the 6 malformed ELFs from #2615's verification table still all exit cleanly.

@prabhakarpujeri
prabhakarpujeri requested a review from a team as a code owner August 20, 2026 06:11
@github-actions github-actions Bot added c dracut-install Issues related to dracut install labels Aug 20, 2026
@devkontrol

Copy link
Copy Markdown
Collaborator

CC @chewi

Comment thread src/install/dracut-install.c Resolved
devkontrol
devkontrol previously approved these changes Aug 23, 2026
@devkontrol devkontrol added this to the 113 milestone Aug 23, 2026
@LaszloGombos
LaszloGombos requested a lite review from Copilot August 25, 2026 13:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens dracut-install’s ELF parsing logic to prevent out-of-bounds reads while walking section/program headers and extracting dynamic strings (RUNPATH/RPATH, SONAME, NEEDED, INTERP), improving robustness against malformed or truncated ELF inputs during dracut-install -l dependency resolution.

Changes:

  • Adds elf_sect_name() and elf_map_string() helpers to bounds-check section-name lookups and mapped NUL-terminated strings.
  • Strengthens PARSE_ELF_START by validating the full section header table extent and the .shstrtab extent before dereferencing.
  • Updates RUNPATH / SONAME / NEEDED / INTERP paths to use the new helpers and adds additional per-iteration bounds checks.
Suppressed comments (4)

src/install/dracut-install.c:1089

  • In RESOLVE_DEPS_DLOPEN_FOR_BITS, the for-loop condition reads d->d_tag before the bounds check inside the loop body. This can still read past the mapping on malformed input (even though the body has a guard).
                for (Elf##B##_Dyn *d = dyn; !soname && ELF_BYTESWAP(32, d->d_tag) != DT_NULL; d++) { \
                        if ((char *)d < (char *)map || (char *)d + sizeof(Elf##B##_Dyn) > (char *)map + src_len) \
                                break; \
                        if (ELF_BYTESWAP(B, d->d_tag) != DT_SONAME) \
                                continue; \

src/install/dracut-install.c:1096

  • Like the RUNPATH path, the DT_SONAME string offset uses sh_offset + d_val without overflow protection. A wraparound can make a bogus offset look in-range and return the wrong in-map string.
                        soname = elf_map_string((const char *)map, src_len, ELF_BYTESWAP(B, shdr[ELF_BYTESWAP(32, shdr[i].sh_link)].sh_offset) + ELF_BYTESWAP(B, d->d_un.d_val)); \
                        if (!soname) \
                                break; \

src/install/dracut-install.c:1179

  • In RESOLVE_DEPS_NEEDED_FOR_BITS, the DT_NULL loop termination condition still dereferences d->d_tag before verifying d is within the mapped file. This undermines the new per-iteration bounds check for malformed/truncated dynamic sections.
                for (Elf##B##_Dyn *d = dyn; ELF_BYTESWAP(32, d->d_tag) != DT_NULL; d++) { \
                        if ((char *)d < (char *)map || (char *)d + sizeof(Elf##B##_Dyn) > (char *)map + src_len) \
                                break; \
                        if (ELF_BYTESWAP(B, d->d_tag) != DT_NEEDED) \
                                continue; \

src/install/dracut-install.c:1186

  • The DT_NEEDED string address is formed by adding sh_offset + d_val without checking for overflow. On wraparound, elf_map_string() can accept the wrapped offset and return an unrelated in-map string.
                        const char *soname = elf_map_string((const char *)map, src_len, ELF_BYTESWAP(B, shdr[ELF_BYTESWAP(32, shdr[i].sh_link)].sh_offset) + ELF_BYTESWAP(B, d->d_un.d_val)); \
                        if (!soname) \
                                break; \

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/install/dracut-install.c Outdated
Comment on lines 923 to 926
for (Elf##B##_Dyn *d = dyn; ELF_BYTESWAP(32, d->d_tag) != DT_NULL; d++) { \
if ((char *)d < (char *)map || (char *)d + sizeof(Elf##B##_Dyn) > (char *)map + src_len) \
break; \
if (ELF_BYTESWAP(B, d->d_tag) == DT_RUNPATH) \

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop condition is already safe here: the (char *)d + sizeof(Elf##B##_Dyn) <= (char *)map + src_len check runs first and && short-circuits, so d->d_tag is only dereferenced after the current entry has been proven to lie fully inside the mapping. The same guard order is used at the other two DT loops. No change needed for this one.

Comment thread src/install/dracut-install.c Outdated
Comment on lines +934 to +936
const char *runpath = elf_map_string((const char *)map, src_len, ELF_BYTESWAP(B, shdr[ELF_BYTESWAP(32, shdr[i].sh_link)].sh_offset) + ELF_BYTESWAP(B, d->d_un.d_val)); \
if (!runpath) \
break; \

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 3c101aa: the string-table offset and d->d_un.d_val are now each clamped to the mapped range before the addition, so the sum passed to elf_map_string() can no longer wrap. Applied at all three call sites the comment flagged (RUNPATH, SONAME, and NEEDED).

@prabhakarpujeri

Copy link
Copy Markdown
Author

Follow-up commit 155d583 addresses the two Copilot notes on the .dynamic walks:

  1. DT_NULL loop condition dereferencing d->d_tag before the bounds check — real and now fixed. The bounds check moved into the loop condition (before the dereference), and the .dynamic section offset is compared against the file length before the pointer is formed. Reproduced as a deterministic SIGSEGV on the old code with a crafted ELF whose .dynamic section ends exactly at the mmap boundary (LD_PRELOAD placing a PROT_NONE guard page after file mmaps); the fix handles it cleanly.
  2. sh_offset + d_val arithmetic overflow — wrapped sums cannot escape the mapping: elf_map_string() validates the final offset against the mapped length and returns NULL otherwise. Worst case is resolving a bogus-but-in-map RUNPATH string, i.e. a wrong lookup, not an out-of-bounds read. Left as is intentionally to keep the code readable.

Sanity-checked: dependency resolution of real binaries (/bin/bash etc.) works unchanged.

@devkontrol devkontrol modified the milestones: 113, 114 Nov 14, 2026
Prabhakar Pujeri added 4 commits November 13, 2026 23:47
The PARSE_ELF_START macro checked only that e_shoff and e_shstrndx are
individually < src_len, then indexed shdr[e_shstrndx].sh_offset into the
mmap'ed file. Two classes of OOB reads remain on truncated or malformed
ELFs:

1. The section header table extent is not validated: e_shnum section
   headers starting at shoff can extend past the end of the file, so
   iterating them reads past the map.
2. The section header string table (shstrtab) content extent isn't
   validated: sh_offset/sh_size come from the file, and the
   strcmp(&shstrtab[shdr[i].sh_name], ...) reads at attacker-controlled
   offsets, potentially past the mapping.

Add the missing bounds checks:

- PARSE_ELF_START now verifies the whole section table fits in the
  mapped file, then treats it as untrusted: a helper reads the shstrtab
  section's offset and size, validates both against src_len, and the
  section-name lookup checks the name offset and NUL termination inside
  the table before any strcmp.
- DRUNPATH/DT_SONAME/DT_NEEDED walks now validate the shadow-section
  link index (sh_link < e_shnum) before dereferencing shdr[sh_link].

Verified by crafting five malformed ELF64 inputs to the new
boundschecks and feeding them to the dependency resolution path:
- Off-map shstrtab.sh_offset: segfault without patch, clean skip with
- sh_name past table end:                    segfault without patch
- No NUL within name's table window:             segfault without patch
- Section table extent truncated (F1) / sh_link past end (F6b): clean
  rejection with patch
Regression control: -l dependency resolution of /bin/bash and /bin/ls
yields byte-identical results before/after. Also verified syntax with
and without HAVE_SYSTEMD (covers RESOLVE_DEPS_DLOPEN_FOR_BITS).
The FIND_LIBRARY_RUNPATH_FOR_BITS macro lacked the bounds checks its
sibling macros (RESOLVE_DEPS_NEEDED_FOR_BITS, RESOLVE_DEPS_DLOPEN_FOR_BITS)
have:

- the .dynamic section's shdr[]-indexed pointer into the map
- the per-iteration Elf_Dyn pointer in the DT_NULL-terminated walk
- the interpreter string (PT_INTERP) and DT_SONAME/DT_NEEDED string,
  passed to strdup/strlen with no NUL-termination verification

A malformed ELF could put a non-NUL-terminated runpath right at the end
of the map (reads past it in strlen), or an interpreter offset exactly
== src_len (off by > vs >=) that passed the old bounds check.

Add a shared helper, elf_map_string(), that validates the offset is
inside the map and the string is NUL-terminated there, and use it at
every place the code dereferences a dynamic-entry/segment-list string.
Mirror the per-iteration map-range checks in the RUNPATH walk that the
sibling macros already carry.

Depends on 0549d1c ("fix(dracut-install): validate ELF section-table
bounds"), which introduced elf_sect_name() and the PARSE_ELF_START
extent checks.

Verified with valgrind on crafted malformed ELF64s (pre-patch SIGSEGV
reading past the mmap; post-patch clean) and byte-for-byte identical
dependency sets for real binaries (/bin/bash, /bin/ls, /usr/bin/ssh).
The .dynamic section walk loops dereferenced d->d_tag in the loop
condition before the in-loop bounds check ran. A malformed ELF whose
.dynamic section ends at the mapped region boundary without a DT_NULL
terminator made the condition read past the mapping; with the mapping
placed exactly before an unmapped/reserved page this segfaults
(reproduced with an LD_PRELOAD that places PROT_NONE guard pages after
file mmaps).

Check the entry bounds in the loop condition before dereferencing,
and drop the now-redundant in-loop checks. Also compare the section
offset against the file length before forming the pointer.
The string offset passed to elf_map_string() is computed as sh_offset + d_val with no overflow check.  On malformed input the addition can wrap, and the helper then validates and returns a string at an unrelated in-map offset, changing dependency resolution on corrupt binaries.

Clamp the string-table offset and the dynamic entry's d_val to the mapped range before adding them at the RUNPATH, SONAME, and NEEDED lookup sites, so the sum always stays inside the map.
@devkontrol
devkontrol force-pushed the fix/elf-runpath-bounds branch from 3c101aa to f17dd59 Compare November 14, 2026 04:47
@devkontrol

Copy link
Copy Markdown
Collaborator

Please resolve conflict, marking it draft for now..

@devkontrol
devkontrol marked this pull request as draft November 28, 2026 01:30

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c dracut-install Issues related to dracut install

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants